package com.lagou.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 计算日期处于本年第几周（周一到周日算一周）
 */
public class ParseWeek extends UDF {
    public int evaluate(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try{
            date = format.parse(dateStr);
        } catch(Exception e){
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();

        calendar.setFirstDayOfWeek(Calendar.MONDAY);

        calendar.setTime(date);

        int i = calendar.get(Calendar.WEEK_OF_YEAR);
        return i;
    }
    @Test
    public void test() {
        int week = evaluate("2021-01-03 18:14:01");
        System.err.println("-----" + week + "-----");
    }
}
